package com.example.app.ui.health.User;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.app.R;
import com.example.app.Entity.HeartRateRecord;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class HistoryActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        RecyclerView recyclerView = findViewById(R.id.rv_history);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        List<HeartRateRecord> records = generateSampleData();
        HistoryAdapter adapter = new HistoryAdapter(records);
        recyclerView.setAdapter(adapter);
    }

    private List<HeartRateRecord> generateSampleData() {
        List<HeartRateRecord> records = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());

        // 生成10条示例数据
        cal.add(Calendar.DAY_OF_MONTH, -2);
        records.add(new HeartRateRecord(72, cal.getTime(), "正常"));

        cal.add(Calendar.MINUTE, 15);
        records.add(new HeartRateRecord(115, cal.getTime(), "异常"));

        cal.add(Calendar.HOUR, 3);
        records.add(new HeartRateRecord(130, cal.getTime(), "严重异常"));

        cal.add(Calendar.DAY_OF_MONTH, 1);
        records.add(new HeartRateRecord(68, cal.getTime(), "正常"));

        cal.add(Calendar.MINUTE, 30);
        records.add(new HeartRateRecord(122, cal.getTime(), "异常"));

        cal.add(Calendar.HOUR, 5);
        records.add(new HeartRateRecord(140, cal.getTime(), "严重异常"));
        cal.add(Calendar.DAY_OF_MONTH, -1);
        records.add(new HeartRateRecord(75, cal.getTime(), "正常"));
        cal.add(Calendar.MINUTE, 45);
        records.add(new HeartRateRecord(118, cal.getTime(), "异常"));
        cal.add(Calendar.HOUR, 2);
        records.add(new HeartRateRecord(128, cal.getTime(), "严重异常"));
        cal.add(Calendar.DAY_OF_MONTH, 3);
        records.add(new HeartRateRecord(70, cal.getTime(), "正常"));
        cal.add(Calendar.DAY_OF_MONTH, -2);
        records.add(new HeartRateRecord(72, cal.getTime(), "正常"));

        cal.add(Calendar.MINUTE, 15);
        records.add(new HeartRateRecord(115, cal.getTime(), "异常"));

        cal.add(Calendar.HOUR, 3);
        records.add(new HeartRateRecord(130, cal.getTime(), "严重异常"));

        cal.add(Calendar.DAY_OF_MONTH, 1);
        records.add(new HeartRateRecord(68, cal.getTime(), "正常"));

        cal.add(Calendar.MINUTE, 30);
        records.add(new HeartRateRecord(122, cal.getTime(), "异常"));

        cal.add(Calendar.HOUR, 5);
        records.add(new HeartRateRecord(140, cal.getTime(), "严重异常"));
        cal.add(Calendar.DAY_OF_MONTH, -1);
        records.add(new HeartRateRecord(75, cal.getTime(), "正常"));
        cal.add(Calendar.MINUTE, 45);
        records.add(new HeartRateRecord(118, cal.getTime(), "异常"));
        cal.add(Calendar.HOUR, 2);
        records.add(new HeartRateRecord(128, cal.getTime(), "严重异常"));
        cal.add(Calendar.DAY_OF_MONTH, 3);
        records.add(new HeartRateRecord(70, cal.getTime(), "正常"));
        return records;
    }
    private static class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {
        private final List<HeartRateRecord> records;
        private final SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());

        HistoryAdapter(List<HeartRateRecord> records) {
            this.records = records;
        }

        @Override
        public ViewHolder onCreateViewHolder(android.view.ViewGroup parent, int viewType) {
            android.view.View view = android.view.LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_heart_rate_history, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            HeartRateRecord record = records.get(position);
            holder.tvHeartRate.setText(String.format(Locale.getDefault(), "%d bpm", record.getHeartRate()));
            holder.tvTime.setText(sdf.format(record.getTime()));
            holder.tvStatus.setText(record.getStatus());

            // 设置状态颜色
            switch (record.getStatus()) {
                case "异常":
                    holder.tvStatus.setTextColor(0xFFFFA500); // 橙色
                    break;
                case "严重异常":
                    holder.tvStatus.setTextColor(0xFFFF0000); // 红色
                    break;
                default:
                    holder.tvStatus.setTextColor(0xFF00FF00); // 绿色
            }
        }

        @Override
        public int getItemCount() {
            return records.size();
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvHeartRate, tvTime, tvStatus;

            ViewHolder(android.view.View itemView) {
                super(itemView);
                tvHeartRate = itemView.findViewById(R.id.tv_heart_rate);
                tvTime = itemView.findViewById(R.id.tv_time);
                tvStatus = itemView.findViewById(R.id.tv_status);
            }
        }
    }
}